//配置路由的地方
import vue from "vue";
import vuerouter from "vue-router";
vue.use(vuerouter);
import routes from "./routers";

//重写push|replace
let originPush = vuerouter.prototype.push; //先把原型对象的push保存一份
let originReplace = vuerouter.prototype.replace;
//call|apply区别
//相同点:都可以调用函数一次，都可以篡改函数上下文一次
//不同点：call与apply传递参数：call传递参数用逗号隔开，apply方法执行，传递数组
vuerouter.prototype.push = function (location, resolve, reject) {
  //第一个参数：告诉push方法，你往哪里跳转 第二个参数：成功回调 第三个参数:失败参数
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject);
  } else {
    originPush.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};
vuerouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject);
  } else {
    originReplace.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};

export default new vuerouter({
  routes,
  scrollBehavior() {
    return { y: 0 };
  },
});
